package org.footballmanager.pandafootball.common.util;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils{
    private static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";

    private DateUtils(){}

	public static Date processWithStartOrEnd(Date date, boolean isStart){
        if(date != null){
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            if(isStart){
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
            }else{
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
            }
            return cal.getTime();
        }
        return null;
    }

	/**
	 * 根据所给日期获取30天后的日期
	 * @param date
	 * @return
	 */
	public static Date getNextMonthDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, 30);
		return cal.getTime();
	}
	
	/**
	 * 返回给定日期是星期几
	 * int类型0代表星期天,1代表星期一
	 * @param date
	 * @return
	 */
	public static int getWeekDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		String tomorrow = new DateFormatSymbols().getShortWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
		int weekday = 0;
		if("星期日".equals(tomorrow)){
			weekday = 0;
		}else if("星期一".equals(tomorrow)){
			weekday = 1;
		}else if("星期二".equals(tomorrow)){
			weekday = 2;
		}else if("星期三".equals(tomorrow)){
			weekday = 3;
		}else if("星期四".equals(tomorrow)){
			weekday = 4;
		}else if("星期五".equals(tomorrow)){
			weekday = 5;
		}else if("星期六".equals(tomorrow)){
			weekday = 6;
		}
		return weekday;
	}
	
	/**  
     *  取得指定月份的最后一天  
     *  @param  date  
     *  @return  Date  
     */  
	public static int getMonthEnd(Date date){  
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	} 
	
	/**  
     *  取得当月的日  
     *  @param  date  
     *  @return  Date  
     */  
	public static int getNowDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
        return cal.get(Calendar.DATE);//获取日 
	}
	
	/**
	 * 获取下个月的第一天
	 * @param date
	 * @return
	 */
	public static Date getNextMonthFirstDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DATE, 1);
		return cal.getTime();
	}
	
	/**
	 * 获取下个月的最后一天
	 * @param date
	 * @return
	 */
	public static Date getNextMonthLastDay(Date date){
		Date lastMonth = getNextMonthFirstDay(date);
		Calendar cal = Calendar.getInstance();
		cal.setTime(lastMonth);
		cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
		return cal.getTime();
	}
	
    /**
     * 功能：制定时间增加小时数。
     * @param hours 正值时时间延后，负值时时间提前。
     * @return Date
     */
    public static Date addHours(Date date, int hours){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR, c.get(Calendar.HOUR)+hours);
        return new Date(c.getTimeInMillis());
    }
     
    /***
     *
     * @param date 时间
     * @return  cron类型的日期
     */
    public static String getCron(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
        String formatTimeStr = "";
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }
}

